package com.test03.gbk2utf;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.commons.io.IOUtils;

/*
 * 这个是用来检测gbk编码格式的.java文件，如果有，就转换为utf-8格式，并覆盖
 * 
 */
public class Test01 {
	private static ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

	public static void main(String[] args) throws Exception {
		File parentDir = new File("D:\\java\\workspace_tdp\\d07_xml_tomcat");
		ArrayList<File> subFile = getSubFile(parentDir);

		for (File file : subFile) {
			if (((file.getName().endsWith(".java") || file.getName().endsWith(".txt")))
					&& "GBK".equals(getFilecharset(file))) {
				trans(file);
				System.out.println(file.getName());
			}
		}

	}

	public static void trans(File file) throws Exception {
		// 正确的去解码
		InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file), "GBk");
		// 把解码后放到缓存中
		IOUtils.copy(inputStreamReader, byteArrayOutputStream);
		// 把原读流关闭
		inputStreamReader.close();
		// 把内容写回
		byteArrayOutputStream.writeTo(new FileOutputStream(file));
		byteArrayOutputStream.reset();
	}

	public static ArrayList<File> getSubFile(File dir) throws Exception {
		ArrayList<File> list = new ArrayList<File>();

		File[] dirs = dir.listFiles();
		for (File file : dirs) {
			if (file.isDirectory()) {
				// 如果是文件夹，得到的list集合，再把list结果追加过来
				// 这个也只需要考虑这一层就可以，递归的逻辑就是这样
				list.addAll(getSubFile(file));
			} else {
				list.add(file);
			}
		}
		return list;
	}

	private static String getFilecharset(File sourceFile) {
		String charset = "GBK";
		byte[] first3Bytes = new byte[3];
		try {
			boolean checked = false;
			BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceFile));
			bis.mark(0);
			int read = bis.read(first3Bytes, 0, 3);
			if (read == -1) {
				return charset; // 文件编码为 ANSI
			} else if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) {
				charset = "UTF-16LE"; // 文件编码为 Unicode
				checked = true;
			} else if (first3Bytes[0] == (byte) 0xFE && first3Bytes[1] == (byte) 0xFF) {
				charset = "UTF-16BE"; // 文件编码为 Unicode big endian
				checked = true;
			} else if (first3Bytes[0] == (byte) 0xEF && first3Bytes[1] == (byte) 0xBB
					&& first3Bytes[2] == (byte) 0xBF) {
				charset = "UTF-8"; // 文件编码为 UTF-8
				checked = true;
			}
			bis.reset();
			if (!checked) {
				int loc = 0;
				while ((read = bis.read()) != -1) {
					loc++;
					if (read >= 0xF0)
						break;
					if (0x80 <= read && read <= 0xBF) // 单独出现BF以下的，也算是GBK
						break;
					if (0xC0 <= read && read <= 0xDF) {
						read = bis.read();
						if (0x80 <= read && read <= 0xBF) // 双字节 (0xC0 - 0xDF)
							// (0x80
							// - 0xBF),也可能在GB编码内
							continue;
						else
							break;
					} else if (0xE0 <= read && read <= 0xEF) {// 也有可能出错，但是几率较小
						read = bis.read();
						if (0x80 <= read && read <= 0xBF) {
							read = bis.read();
							if (0x80 <= read && read <= 0xBF) {
								charset = "UTF-8";
								break;
							} else
								break;
						} else
							break;
					}
				}
			}
			bis.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return charset;
	}
}
